package com.webchat.ugc.service.chain;

import com.webchat.common.constants.MomentConstants;
import com.webchat.domain.vo.response.moment.MomentLinkVO;
import com.webchat.domain.vo.response.moment.MomentMediaVO;
import com.webchat.domain.vo.response.moment.MomentVO;
import com.webchat.ugc.repository.dao.IMomentDAO;
import com.webchat.ugc.repository.dao.IMomentLinkDAO;
import com.webchat.ugc.repository.dao.IMomentMediaDAO;
import com.webchat.ugc.repository.entity.MomentEntity;
import com.webchat.ugc.repository.entity.MomentLinkEntity;
import com.webchat.ugc.repository.entity.MomentMediaEntity;
import com.webchat.ugc.service.moment.MomentLinkService;
import com.webchat.ugc.service.moment.MomentMediaService;
import com.webchat.ugc.service.moment.MomentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;


@Slf4j
@Component
public class MomentRefreshHandler implements MomentPublishHandler {


    @Autowired
    private MomentService momentService;
    @Autowired
    private IMomentMediaDAO momentMediaDAO;
    @Autowired
    private IMomentLinkDAO momentLinkDAO;
    @Autowired
    private IMomentDAO momentDAO;

    @Override
    public void handle(MomentVO moment, MomentPublishHandlerChain chain) {
        /**
         * 1. 持久化新生成动态数据
         */
        this.doSaveMomentData(moment);
        /**
         * 2. 重新刷新动态缓存
         */
        momentService.refreshMomentCache(moment.getId());
        chain.handle(moment, chain);
    }


    /**
     * 持久化消息队列消费中获取到的朋友圈动态扩展字段信息
     *
     * @param moment
     */
    private void doSaveMomentData(MomentVO moment) {

        Long momentId = moment.getId();
        MomentEntity momentEntity = momentDAO.findById(momentId).orElse(null);
        if (momentEntity == null) {
            return;
        }
        // 刷主表数据
        momentEntity.setIpAddress(moment.getIpAddress());
        momentEntity.setReviewScore(moment.getReviewScore());
        momentEntity.setStatus(MomentConstants.getStatusByReviewScore(moment.getReviewScore()).getStatus());
        momentDAO.save(momentEntity);
        // 刷新媒体资源数据
        if (ObjectUtils.equals(moment.getIncludeImages(), true)) {
            Set<Long> resourceIds = moment.getImages().stream().map(MomentMediaVO::getId).collect(Collectors.toSet());
            List<MomentMediaEntity> images = momentMediaDAO.findAllById(resourceIds);
            Map<Long, MomentMediaVO> mediaVOMap = moment.getImages().stream().collect(Collectors.toMap(MomentMediaVO::getId, Function.identity()));
            images.forEach(m -> {
                MomentMediaVO momentMediaVo = mediaVOMap.get(m.getId());
                m.setWidth(momentMediaVo.getWidth());
                m.setHeight(momentMediaVo.getHeight());
                m.setSize(momentMediaVo.getSize());
            });
            momentMediaDAO.saveAll(images);
        }
        // 刷新媒体资源数据
        if (ObjectUtils.equals(moment.getIncludeVideo(), true)) {
            MomentMediaVO videoVo = moment.getVideo();
            MomentMediaEntity video = momentMediaDAO.findById(videoVo.getId()).orElse(null);
            video.setWidth(videoVo.getWidth());
            video.setHeight(videoVo.getHeight());
            video.setSize(videoVo.getSize());
            momentMediaDAO.save(video);
        }
        if (ObjectUtils.equals(moment.getIncludeLink(), true)) {
            MomentLinkVO linkVo = moment.getLink();
            MomentLinkEntity momentLink = momentLinkDAO.findById(linkVo.getId()).orElse(null);
            momentLink.setTitle(linkVo.getTitle());
            momentLink.setCover(linkVo.getCover());
            momentLinkDAO.save(momentLink);
        }
    }
}
